package cn.com.xyc.service.impl;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Date;
import java.util.List;

import javax.imageio.ImageIO;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.com.xyc.dao.CostLiveMapper;
import cn.com.xyc.dao.PicGroupInfoMapper;
import cn.com.xyc.dao.PicInfoMapper;
import cn.com.xyc.dao.WordBookMapper;
import cn.com.xyc.pojo.Admin;
import cn.com.xyc.pojo.PicGroupInfo;
import cn.com.xyc.pojo.PicInfo;
import cn.com.xyc.service.ToolService;

@Service
public class ToolServiceImpl implements ToolService {
	@Autowired
	private PicInfoMapper picInfoMapper;
	@Autowired
	private PicGroupInfoMapper picGroupInfoMapper;
	@Autowired
	private WordBookMapper wordBookMapper;
	@Autowired
	private CostLiveMapper costLiveMapper;

	public int insertPicGroupInfo(PicGroupInfo record, Admin user)
			throws Exception {
		record.setCreateDate(new Date());
		record.setCreateUser(user.getId());
		return picGroupInfoMapper.insertSelective(record);
	}

	public List<PicGroupInfo> selectPicGroupInfos() throws Exception {
		return picGroupInfoMapper.selectByExample(null);
	}

	public int insertPicInfo(PicInfo record, Admin user, File imgFile)
			throws IOException {
		record.setPicCreateTime(new Date());
		record.setPicCreateUser(user.getId());
		record.setPicVisible(1);
		BufferedImage image = ImageIO.read(new FileInputStream(imgFile));
		String size = String.format("%.1f", imgFile.length() / 1024.0);
		int width = image.getWidth();
		int height = image.getHeight();

		record.setPicSize(size);
		record.setPicHeight(Integer.toString(height));
		record.setPicWidth(Integer.toString(width));
		return picInfoMapper.insertSelective(record);
	}

	public List<PicInfo> selectPicInfos() throws Exception {
		return picInfoMapper.selectByExample(null);
	}

}
